import { addLayer, LayerType, LayerConfig } from "@/mapbox/mapbox_core/layer/layerManage";
import { getLayerManagerState } from "@/mapbox/mapbox_core/layer/layerManagerState";
/* eslint-disable @typescript-eslint/no-unused-vars */
// 解析，分配点线面
export function geojsonParse(map: any, layerId: string, geojsonData: any, defaultOptions: any) {
  // 添加 GeoJSON 数据源
  map.addSource(layerId, {
    type: "geojson",
    data: geojsonData
  });

  let layerOptions;
  const geometryType = getGeometryType(geojsonData);

  switch (geometryType) {
    case "Point":
      layerOptions = {
        type: "circle",
        paint: {
          "circle-color": defaultOptions.pointColor || "blue",
          "circle-radius": defaultOptions.pointRadius || 5
        },
        ...defaultOptions
      };
      break;

    case "LineString":
      layerOptions = {
        type: "line",
        paint: {
          "line-color": ["get", "valueColor"] || defaultOptions.lineColor || "green",
          "line-width": ["get", "lineWidth"] || defaultOptions.lineWidth || 2
        },
        ...defaultOptions
      };

      // 添加文字标签（symbol 图层）
      const textLayerId = `${layerId}-text`;
      const textLayer = {
        id: textLayerId,
        type: "symbol",
        source: layerId,
        layout: {
          "text-field": ["get", "value"] || "", // 从属性获取文字内容
          "text-font": ["MicrosoftYaHei"],
          "text-size": defaultOptions.textSize || 12,
          "text-anchor": "center", // 文字锚点位置
          "text-offset": [0, 0], // 文字偏移
          "text-rotate": ["get", "textRotate"] || 0, // 文字旋转角度
          "symbol-placement": "line-center", // 沿线路居中显示
          "symbol-spacing": 200, // 符号间距
          "text-allow-overlap": false, // 是否允许文字重叠
          "text-ignore-placement": false
        },
        paint: {
          "text-color": defaultOptions.textColor || "#333",
          "text-halo-color": defaultOptions.textHaloColor || "#fff",
          "text-halo-width": defaultOptions.textHaloWidth || 1,
          "text-opacity": defaultOptions.textOpacity || 1
        }
      };

      // 如果需要添加文字图层，返回两个图层配置
      if (defaultOptions.showText !== false) {
        map.addLayer(textLayer);
      }
      break;

    case "Polygon":
      layerOptions = {
        type: "fill",
        paint: {
          "fill-color": ["get", "fill"] || defaultOptions.fillColor || "red",
          "fill-opacity": ["get", "fill-opacity"] || defaultOptions.fillOpacity || 0.5,
          "fill-outline-color": ["get", "stroke"] || defaultOptions.strokeColor || "black",
          "fill-outline-opacity": ["get", "stroke-opacity"] || defaultOptions.strokeOpacity || 1
        },
        ...defaultOptions
      };
      break;

    default:
      console.error("Unsupported geometry type in GeoJSON");
      return;
  }

  // 添加基础图层
  map.addLayer({
    id: layerId,
    ...layerOptions,
    source: layerId
  });

  return {
    id: layerId,
    geometryType,
    layerOptions
  };
}

// 函数getGeometryType用于获取geojson对象的几何类型
function getGeometryType(geojson) {
  // 如果geojson对象的类型为FeatureCollection且features数组长度大于0
  if (geojson.type === "FeatureCollection" && geojson.features.length > 0) {
    // 返回features数组中第一个元素的geometry.type
    return geojson.features[0].geometry.type;
    // 如果geojson对象的类型为Feature
  } else if (geojson.type === "Feature") {
    // 返回geometry.type
    return geojson.geometry.type;
  }
  // 如果不符合上述条件，返回null
  return null;
}

// 加载geojson 图标
export function loadGeojsonIcon(map: any, id: string, geojsonData: any) {
  const source = {
    type: LayerType.geojson,
    data: geojsonData
  };
  // 添加图层显示点标记
  const layerOptions: LayerConfig = {
    id: id,
    type: LayerType.Symbol,
    source,
    layout: {
      "icon-image": "{iconName}", // 使用Mapbox默认图标
      "icon-size": 0.4
    },
    paint: {
      "icon-color": "#2262CC" // 自定义图标颜色
    }
  };
  addLayer(getLayerManagerState(map), layerOptions);
}
